/**
 * 
 */
package calendarInterface2;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import calendarEventManager.CEMAgenda;


import controller.CTLEngine;

/**
 * @author Mohamed Ali Ayed
 *
 */
@SuppressWarnings("serial")
public class FUpdateAgenda extends  FrameAgenda{

	private JPanel pListAgenda;
	private JComboBox<String> JCBAgenda;
	private JLabel lAgenda;
	private JButton bUpdate;
	private JButton bDelete;
	private JPanel pContainer;
	private int idAgdendaUpdate=0;
	/**
	 * 
	 */
	public FUpdateAgenda () {
		super();
		this.setTitle("Update Agenda");
		setSize(300, 100);
		setLocationRelativeTo(null); //center the window
		bSaveAgenda.addActionListener(new ActionUpdateAgenda());
		bCancelAgenda.addActionListener(new ActionHideFrameAgenda());
		addWindowListener (new WindowAdapter() {
	          public void windowClosing(WindowEvent e) {
	      		CalendarInterface.hideFrameUpdateAgenda();
	             }
	          }
		);
		buildFrame();
		//this.add(pAgenda, BorderLayout.CENTER);
	}

	private void buildFrame() {
		pContainer = new JPanel(new BorderLayout());
		pListAgenda = new JPanel(new GridLayout(2, 2));
		//pListAgenda.setSize(300, 100);
		buildPanel2(pListAgenda);
		pContainer.add(pListAgenda, BorderLayout.NORTH);
		this.add(pContainer);
	}

	private void buildPanel2(JPanel panel) {
		lAgenda = new JLabel("Agenda",JLabel.CENTER);
		panel.add(lAgenda);
		JCBAgenda = new JComboBox<String>();
		JCBAgenda.addItem("choose an agenda");
		List<CEMAgenda> listAgenda = CTLEngine.getAllAgendas();
		
		for (CEMAgenda oAgenda : listAgenda){
			JCBAgenda.addItem(oAgenda.getAgendaName());
		}
		panel.add(JCBAgenda);
		bUpdate = new JButton("Update");
		bUpdate.addActionListener(new ActionShowPanelUpdate());
		panel.add(bUpdate);
		bDelete = new JButton("Delete");
		bDelete.addActionListener(new ActionDeleteAgenda());
		panel.add(bDelete);
		
	}
	
	class ActionHideFrameAgenda implements ActionListener{
		 
        public void actionPerformed(ActionEvent e) {
        	CalendarInterface.hideFrameUpdateAgenda();
        }// end actionPerformed               
	}//end classe interne
	
	class ActionUpdateAgenda implements ActionListener{
		 
        public void actionPerformed(ActionEvent e) {
        	if (tName.getText().equalsIgnoreCase("")){
        		OPError messError = new OPError();
        		messError.showErrorWindow("Error Name", "Erreur Saisie");
        	}
        	else {

       		CalendarInterface.updateAgenda(idAgdendaUpdate, tName.getText(),(String) CBColor.getSelectedItem() );
        	}
        }// end actionPerformed               
	}//end classe interne
	
	class ActionShowPanelUpdate implements ActionListener{
		 
        public void actionPerformed(ActionEvent e) {
        	if (JCBAgenda.getSelectedItem()=="choose an agenda"){
        		OPError messError = new OPError();
        		messError.showErrorWindow("Error choose", "error");
        	}
        	else {
         		setSize(300,300); 
        		setLocationRelativeTo(null);
        		tName.setText((String)JCBAgenda.getSelectedItem());
        		CBColor.setSelectedIndex(CTLEngine.getAgenda(CTLEngine.getIdAgenda((String)JCBAgenda.getSelectedItem())).getAgendaColour());
        		idAgdendaUpdate = CTLEngine.getIdAgenda((String)JCBAgenda.getSelectedItem());
        		pAgenda.updateUI();
        		pContainer.add(pAgenda, BorderLayout.CENTER);
            	pContainer.updateUI();
        	}
        	
   
        	
        }// end actionPerformed               
	}//end classe interne
	
	class ActionDeleteAgenda implements ActionListener{
		 
        public void actionPerformed(ActionEvent e) {
        	if (JCBAgenda.getSelectedItem()=="choose an agenda"){
        		OPError messError = new OPError();
        		messError.showErrorWindow(messError.NO_AGENDA_SELECTED_MSG,messError.ERROR_FRAME);
        	}
        	else {
        		CalendarInterface.deleteAgenda((String)JCBAgenda.getSelectedItem());
        	}
        	
        }// end actionPerformed               
	}//end classe interne
}
